<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Mod_Admin extends CI_Controller {
    private $tb_hotrotructuyen = 'hotro';
    private $diadiem ='diadiem';
     //phan trang
    private $base_url_trang = '#';
    private $total_rows = 100;
    private $per_page =5;
    private $limit = 5;
    private $thongtinbenh = 'thongtinbenh';

	public function index(){      
       // $this->run();
        //$this->load->helper(array('form', 'text'));
		//$this->lang->load('tank_auth'); 
        //$this->load->library('session');
        //$this->load->model('mod_admin/admin_model');
        //$this->load->library(array('pagination','form_validation','security'));
        $this->load->view('vmod_admin');
	}
    function xuly(){
        $data['page']= $this->run();
        $this->load->view('vmod_admin',$data);
    }
    function run(){
       
       $rounte = $this->security->xss_clean($this->uri->segment(3,0));//su chu run
       $dk = $this->security->xss_clean($this->uri->segment(4,0)) ;//sau chu run
       $id = $this->security->xss_clean($this->uri->segment(5,0));//lay id
       if(!is_numeric($id)){$id=0;}
       if(!empty($this->$rounte)){$table= $this->$rounte;$page=$this->$rounte;}else $table='';//chua kiem tra co bang hay chua
       if(!empty($dk)){
            if($dk=='list'){
                if(!empty($_POST)){
                    $data = $this->security->xss_clean($_POST);
                    $data = $this->xuly_dulieu($data,$table);
                    if($id){
                        $this->db->where('id', $id);
                        $this->db->update($table, $data); 
                        $this->session->set_flashdata('thongbao', 'Đã Sửa xong');
                    }else{
                        $this->db->insert($table, $data); 
                        $this->session->set_flashdata('thongbao', 'Đã thêm');
                    }
                }
                //hien thi danh sach
                $this->base_url_trang = base_url().'mod_admin/xuly/'.$page.'/list';
                if($table != $this->session->userdata('sec_table')){
                    //check xem co trung bang hay khong de kiem tra xem dang o trang cu hoac trang moi xoa session//bang ba
                    // bang bang session bang thi kiem tra order_by
                    $this->session->set_userdata('order',0);
                    $this->session->set_userdata('sec_table',$table);
                }
                if($this->session->userdata('order')){
                        $order = array('truong'=>$this->session->userdata('order'),'sapxep'=>$this->session->userdata('sapxep'));
                }else $order=array();
                
                 // limit start dung sau 
                if(!$this->session->userdata('limit')){$this->session->set_userdata('limit',$this->limit);}else $this->limit = $this->session->userdata('limit');
                $this->per_page = $this->limit;// so record tren 1 trang bang limit                
               if($id){
                     $srart_limit=$id;
                }else $srart_limit=0;
                $limit = $this->limit;
                
                //where
                $where = array();
                $data1 = $this->home_model->get_data($table,$where,$order,$limit,'',$srart_limit);
                $data= array('data'=>$data1);
                $this->total_rows =$this->home_model->get_number($table,$where='',$limit='');
                $this->phantrang();
                return $this->load->view($page.'_list',$data,true);//true la k hien chi tra ve
            }elseif($dk=='edit'){
                //hoien thi sua
                if($id){
                    $data =array('data'=>$this->home_model->get_one($table,array('id'=>$id)));
                    return $this->load->view($page.'_edit',$data,true);//true la k hien chi tra ve
                }else{
                    $data = array();
                    return $this->load->view($page.'_edit',$data,true);//true la k hien chi tra ve
                }
            }elseif($dk=='xoa'){
               if($id){
                $this->db->delete($table, array('id' => $id)); 
                $this->session->set_flashdata('thongbao', 'Đã thực hiện');
               }
            }
       }
      
    }
    function phantrang(){
        $this->load->library('pagination');
        $config['base_url'] = $this->base_url_trang;
        $config['total_rows'] = $this->total_rows;
        $config['per_page'] = $this->per_page;
        $config['uri_segment'] = 5;
        $config['num_links'] = 12;       
        //-----
        $this->pagination->initialize($config);
    
    }
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */